/*
 * Copyright 2009 Prime Technology.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.primefaces.examples.view;

import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.examples.domain.Player;

@ManagedBean(name = "autoCompleteBean")
@SessionScoped
public class AutoCompleteBean {

    private Player selectedPlayer1;
    private List<Player> players;

    public AutoCompleteBean() {
        players = new ArrayList<Player>();

        players.add(new Player("Messi", 100));
        players.add(new Player("Ibrahimovic", 9));
        players.add(new Player("Henry", 14));
        players.add(new Player("Iniesta", 8));
        players.add(new Player("Xavi", 6));
        players.add(new Player("Puyol", 5));
    }

    public Player getSelectedPlayer1() {
        return selectedPlayer1;
    }

    public void setSelectedPlayer1(Player selectedPlayer1) {
        this.selectedPlayer1 = selectedPlayer1;
    }

    public List<Player> completePlayer(String query) {
        List<Player> suggestions = new ArrayList<Player>();

        for (Player p : players) {

            if (p.getName().startsWith(query)) {
                suggestions.add(p);
            }
        }

        return suggestions;
    }
}
